<template>
    <el-card>
        <el-form :model="queryParams" ref="queryParams" :inline="true" label-width="100px">
            <el-form-item label="名称" prop="app">
                <el-input v-model="queryParams.app" placeholder="名称" size="small"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="appName">
                <el-input v-model="queryParams.appName" placeholder="描述" size="small"></el-input>
            </el-form-item>
            <el-form-item >
                <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-delete" size="small" @click="resetQuery('queryParams')">清空</el-button>
            </el-form-item>
        </el-form>
         <el-row :gutter="10">
            <el-col :span="24">
                <div style="margin-bottom: 10px">
                    <el-button type="primary" icon="el-icon-plus" size="small" @click="handleAdd">新增</el-button>
                </div>
            </el-col>
        </el-row>
        <el-table :data="tableData" border style="width: 100%" size="small">
            <el-table-column type="index" label="#" width="50"></el-table-column>
            <el-table-column label="名称" prop="name"></el-table-column>
            <el-table-column label="描述" prop="description"></el-table-column>
            <el-table-column label="类型" prop="type">
                 <template slot-scope="{row}">
                    <span v-if="row.type===1">Eureka</span>
                    <span v-if="row.type===1">Nacos</span>
                </template>
            </el-table-column>
            <el-table-column label="健康状态" prop="health">
                 <template slot-scope="{row}">
                    {{row.health|healthFilter}}
                </template>
            </el-table-column>
            <el-table-column label="操作" width="300" align="center">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDel(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
         <el-pagination
                v-show="total>0"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                background
                :current-page="1"
                :page-sizes="[10, 20, 30, 40,50,100]"
                :page-size="10"
                layout="total, sizes, prev, pager, next, jumper"
                class="pagination-container"
                :total="total">
            </el-pagination>
     
         <el-dialog :title="title" :visible.sync="formVisible" :rules="rules" width="40%"  @close="resetForm('form')">
            <el-form :model="form" :rules="rules" ref="form" :disabled="mode==='view'">
                <el-row :gutter="20">
                  <el-col :span="24">
                      <el-form-item  prop="name" label="名称" :label-width="formLabelWidth" required>
                          <el-input v-model="form.name" autocomplete="off" size="mini" placeholder="请输入名称" :disabled="mode==='edit'"></el-input>
                          <span class="tips">名称可自定义,网关会根据该名称来获取注册中心的配置,如：Eureka_1;保存后名称不能修改</span>
                      </el-form-item>
                  </el-col>
                </el-row>
                <el-row :gutter="20">
                  <el-col :span="24">
                      <el-form-item  prop="description" label="描述" :label-width="formLabelWidth">
                          <el-input v-model="form.description" type="textarea" rows="6" autocomplete="off" size="mini" placeholder="请输入描述"></el-input>
                      </el-form-item>
                  </el-col>
                </el-row>
                 <el-row :gutter="20">
                  <el-col :span="24">
                      <el-form-item  prop="type" label="类型" :label-width="formLabelWidth" required>
                            <el-radio-group v-model="form.type">
                                <el-radio :label="1">Eureka</el-radio>
                                <el-radio :label="2">Nacos</el-radio>
                            </el-radio-group>
                      </el-form-item>
                  </el-col>
                </el-row>
                 <el-row :gutter="20">
                  <el-col :span="24">
                        <el-form-item  prop="format" label="配置格式" :label-width="formLabelWidth" required>
                            <el-radio-group v-model="form.format">
                                <el-radio :label="1">yml</el-radio>
                                <el-radio :label="2">properties</el-radio>
                            </el-radio-group>
                        </el-form-item>
                  </el-col>
                </el-row>
                 <el-row :gutter="20">
                  <el-col :span="24">
                      <el-form-item  prop="content" label="配置内容" :label-width="formLabelWidth" required>
                          <el-input v-model="form.content" type="textarea" rows="6" autocomplete="off" size="mini" placeholder="请输入配置内容"></el-input>
                      </el-form-item>
                  </el-col>
                </el-row>
            </el-form>
               <div slot="footer" class="dialog-footer" v-if="mode!=='view'">
                    <el-button type="primary" @click="handleSubmit('form')" size="small">保存</el-button>
                    <el-button @click="formVisible = false" size="small">取消</el-button>
               </div>
          </el-dialog>
   </el-card>
</template>

<script>
import { getRegistryList,saveRegisterForm,deleteRegister,update } from '@/api/proxy/config.api.js'
    export default {
        data() {
            return {
                  queryParams: {
                      current: 1,
                      size: 10
                  },
                  tableData: [],
                  total: 0,
                  
                  title: '新增注册中心',
                  formVisible: false,
                  form: {},
                  formLabelWidth: "120px",
                  rules: {
                    name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
                    type: [{ required: true, message: '请选择类型', trigger: 'blur' },],
                    format: [{ required: true, message: '请选择格式', trigger: 'blur' }, ],
                    content: [{ required: true, message: '请选择格式', trigger: 'blur' }],
                  },
                  mode: 'add'
            }
        },
        mounted() {
            this.getList();
        },
        filters: {
            healthFilter(value) {
                return value?'健康':'异常'
            }
        },
        methods: {
            async getList() {
                const result = await getRegistryList(this.queryParams);
                if(result.code === 200) {
                    this.tableData = result.data.records;
                    this.total = result.data.total
                }
            },
              // 搜索
            handleQuery() {
                this.queryParams.current = 1;
                this.getList();
            },
             resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            // 清空
            resetQuery(formName) {
                this.$refs[formName].resetFields();
                this.getList();
            },
            handleSizeChange(val) {
                this.queryParams.size = val;
                this.getList();
            },
            handleCurrentChange(val) {
                this.queryParams.current = val;
                this.getList();
            },
            handleAdd() {
                this.title = '新增注册中心';
                 this.mode = 'add';
                this.formVisible = true;
            },
            handleView(row) {
                this.title = '查看注册中心';
                this.mode = 'view';
                 this.form =row;
                this.formVisible = true;
            },
            handleEdit(row) {
                this.title = '编辑注册中心';
                this.form =JSON.parse(JSON.stringify(row));
                this.mode = 'edit';
                this.formVisible = true;
            },
             handleDel(id) {
                this.$confirm('确定将选择的数据删除?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                    }).then(async () => {
                        const result = await deleteRegister(id);
                        if(result.code === 200) {
                             this.$message({
                                type: 'success',
                                message: '删除成功!'
                           });
                           this.getList();
                        }
                    }).catch((err) => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });          
                });
            },
             handleSubmit(formName) {
               this.$refs[formName].validate(async (valid) => {
                 if (valid) {
                    if(this.form.id) {
                        const result = await update(this.form);
                         if(result.code===200) {
                            this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                         }
                    }else{
                         const result = await saveRegisterForm(this.form);
                         if(result.code===200) {
                            this.$message({
                                message: '保存成功',
                                type: 'success'
                            });
                        }
                    }
                        this.formVisible = false;
                        this.getList();
                 } else {
                    console.log('error submit!!');
                    return false;
                 }
                });
            },
        }
    }
</script>

<style lang="scss" scoped>
.info {
    font-size: 10px;
    opacity: .7;
}
.pagination-container {
    padding: 10px;
    text-align: right;
}
.tips {
    color: #999;
    font-size: 12px;
    line-height: normal;
    margin-top: 8px;
    display: block; 
}
</style>